package com.xl.sst.controller;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xl.sst.biz.GoodsBiz;
import com.xl.sst.vo.BigClass;
import com.xl.sst.vo.Goods;
import com.xl.sst.vo.Page;
import com.xl.sst.vo.SmallClass;
import com.xl.sst.vo.TableNum;
import com.xl.sst.vo.Users;

/*
 * 商品处理层
 * yue
 */
@Controller
@RequestMapping("/view")
public class GoodsController {
	@Resource
	private GoodsBiz GoodsBiz;
	public GoodsBiz getGoodsBiz() {
		return GoodsBiz;
	}
	public void setGoodsBiz(GoodsBiz goodsBiz) {
		GoodsBiz = goodsBiz;
	}	
	/*
	 * 获取页面传递的值进行分页分类查询
	 * 页面请求需要两个参数
	 * index 当前页
	 * category   类别
	 */
	@RequestMapping("/tableNum.do")
	public @ResponseBody Map<String,Object> tableNum(Page page){
		Map<String,Object> m=new HashMap<String, Object>();
		//当前页
		int nowPage =1;
		//每页条数
		int rows=2;
		//总页数
		int sumPage=1;
		//获取页面当前页并传递
		if(page !=null){
			nowPage=page.getIndex();
		}		
		//获取分页数并传递
		int count=GoodsBiz.getCount(page);
		if(count%rows==0){
			sumPage=count/rows;
		}else{
			sumPage=count/rows+1;
		}				
		Page p=new Page();
		p.setIndex(rows*(nowPage-1));
		p.setRow(rows);
		p.setCategory(page.getCategory());
		//获取商品
		List<Goods> list=GoodsBiz.selectAllByPage(p);
		TableNum t=new TableNum();
		t.setPage(nowPage);	
		t.setTotalPage(sumPage);
		t.setTotalSize(count);
		t.setList(list);
		m.put("tableNum", t);
		return m;		
	}
	/*
	 * 添加商品
	 * uploadfile文件上传表单的name属性
	 */
	@RequestMapping("/goodsadd.do")
	public String upload(@RequestParam(value="uploadfile") MultipartFile file,HttpServletRequest request,Goods g){
			//设置要上传的文件路径  在服务器创建 images文件夹	
			
	        String path = request.getSession().getServletContext().getRealPath(""); 
	        path=path.substring(0, path.lastIndexOf("\\"));
	        String newpath=path+"\\goods\\images";
	        //保存路径
	        File images=new File(newpath);
	        //文件名	          
	        long s = new Date().getTime();  
	        //当前时间的毫秒值和原文件名拼接
	        //如果原文件名过长则截取后10位
	        String p=file.getOriginalFilename(); 
	        if(p.length()>10){
	        	p= p.substring(p.length()-10, p.length());
	        }
	        String fileName =s+ p;  
	        File targetFile = new File(images, fileName);
	        //文件夹不存在则创建
	        if(!targetFile.exists()){  
	            targetFile.mkdirs();  
	        }  	  
	        //保存  
	        try {  
	            file.transferTo(targetFile);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	        //业务处理
	        //获取创建人id
	        //Users u=(Users)request.getAttribute("user");			
			//g.setCreatUser(u.getUserID());
	        g.setGoods_dimage(fileName);
	        if(GoodsBiz.add(g)){
	        	return "添加成功";  
	        }else{
	        	return "添加失败";  
	        }
	}
		
	/*
	 * 服务商上传的所有商品
	 * 测试
	 * http://localhost:8080/SST/view/goodsAllByShop.do?category=1
	 */
	@RequestMapping("/goodsAllByShop.do")
	public String  goodsAllByShop(Page page,HttpServletRequest req,Map<String,Object> m){
		//当前页
		int nowPage =1;
		//每页条数
		int rows=2;
		//总页数
		int sumPage=1;
		//获取页面当前页并传递
		if(page.getIndex() !=null){
			nowPage=page.getIndex();
		}		
		//获取分页数并传递
		int count=GoodsBiz.getCountByShop(page);
		if(count%rows==0){
			sumPage=count/rows;
		}else{
			sumPage=count/rows+1;
		}				
		Page p=new Page();
		p.setIndex(rows*(nowPage-1));
		p.setRow(rows);
		//Category这里是服务商的id  需要取session的数据
		//Users u=(Users)req.getSession().getAttribute("user");
		//u.getUserID();
		p.setCategory(page.getCategory());
		m.put("sumPage", sumPage);
		m.put("nowPage", nowPage);
		m.put("count", count);
		//获取商品
		m.put("goodsAllByShop", GoodsBiz.selectAllByShop(p));
		//获取小分类
		m.put("getAllSmallClass", GoodsBiz.getAllSmallClass());
		return "goodsshop.jsp";
	}
	/*
	 * 删除商品
	 */
	@RequestMapping("/goodsdelete.do")
	public @ResponseBody Map<String,Object> delete(Goods g){
		Map<String,Object> m=new HashMap<String, Object>();
		int i=GoodsBiz.delete(g);
		if(i>=1){
			m.put("goodsdelete", "成功删除"+i+"条商品！");
		}else{
			m.put("goodsdelete", "删除失败！");
		}
		return m;			
	}
	/*
	 * 修改商品
	 */
	@RequestMapping("/goodsupdate.do")
	public @ResponseBody String update(@RequestParam(value="loadfile") MultipartFile file,HttpServletRequest request,Goods g){
		//设置要上传的文件路径  在服务器创建 images文件夹	
		
        String path = request.getSession().getServletContext().getRealPath(""); 
        path=path.substring(0, path.lastIndexOf("\\"));
        String newpath=path+"\\goods\\images";
        //保存路径
        File images=new File(newpath);
        //文件名	          
        long s = new Date().getTime();  
        //当前时间的毫秒值和原文件名拼接
        //如果原文件名过长则截取后10位
        String p=file.getOriginalFilename(); 
        if(p.length()>10){
        	p= p.substring(p.length()-10, p.length());
        }
        String fileName =s+ p;  
        File targetFile = new File(images, fileName);  
        //保存  
        try {  
            file.transferTo(targetFile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        //业务处理
        g.setGoods_dimage(fileName);
		if(GoodsBiz.update(g)){
			return"修改商品成功！";			
		}else{
			return "修改商品失败！";
		}	
	}
	/*
	 * 修改商品发送原始数据到页面
	 */
	@RequestMapping("/goodsupdateone.do")
	public String updateOne(Goods g,Map<String,Object> m){
		m.put("goodsupdateone", GoodsBiz.selectById(g));
		return "goodsupdate.jsp";			
	}
	/*
	 * 获取商品大分类
	 * 测试
	 * http://localhost:8080/SST/view/getBigClass.do
	 */
	@RequestMapping("/getBigClass.do")
	public  @ResponseBody List<BigClass> getBigClass(){		
		return GoodsBiz.getBigClass();			
	}
	/*
	 * 根据大分类id获取商品小分类
	 * 测试
	 * http://localhost:8080/SST/view/getSmallClass.do?bigclassId=1
	 */
	@RequestMapping("/getSmallClass.do")
	public  @ResponseBody List<SmallClass> getSmallClass(SmallClass s){		
		return GoodsBiz.getSmallClass(s);			
	}
	/*
	 * 根据id获取商品小分类
	 * 测试
	 * http://localhost:8080/SST/view/getSClassById.do?id=1
	 */
	@RequestMapping("/getSClassById.do")
	public  @ResponseBody SmallClass getSClassById(SmallClass s){		
		return GoodsBiz.getSClassById(s);			
	}
}
